<template>
  <div class="articles-container">
    <el-card>
      <!-- 面试技巧，页面布局搭建 -->
      <el-row class="articlesChoice">
        <el-col :span="18">
          <el-form
            :inline="true"
            :model="reqParams"
            class="demo-form-inline"
            label-width="80px"
          >
            <el-form-item label="关键字" size="small">
              <el-input
                v-model="reqParams.keyword"
                placeholder="根据文章标题搜索"
              />
            </el-form-item>
            <el-form-item label="状态">
              <el-select
                v-model="reqParams.state"
                placeholder="请选择"
                size="small"
              >
                <el-option label="启用" :value="0" />
                <el-option label="禁用" :value="1" />
              </el-select>
            </el-form-item>
            <el-form-item style="text-align: right">
              <el-button size="small" @click.native="delKeyWords()"
                >清除</el-button
              >
              <el-button type="primary" size="small" @click.native="search()"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align: right">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="small"
            @click="addArticle()"
            >新增技巧</el-button
          >
        </el-col>
      </el-row>
      <!-- 数据条数 -->
      <el-alert
        :title="`数据一共 ${total}  条`"
        style="margin-bottom: 15px"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      />
      <!-- 表格 -->
      <el-table :data="tableArticlesList">
        <el-table-column
          type="index"
          label="序号"
          width="80px"
          :index="indexMethod"
        />
        <el-table-column label="文章标题" width="400px">
          <template #default="{ row }">
            {{ row.title }}
            <a
              v-if="row.videoURL"
              href="#"
              @click.prevent="openVideo(row.videoURL)"
              ><i class="el-icon-film"
            /></a>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数" />
        <el-table-column prop="username" label="录入人" />
        <el-table-column prop="createTime" label="录入时间">
          <template #default="{ row }">
            {{ row.createTime | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态" width="180px">
          <template #default="{ row }">
            {{ row.state === 1 ? "已启用" : "已禁用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template #default="{ row }">
            <el-button type="text" @click="previewArticle(row)">预览</el-button>
            <el-button type="text" @click="stateToggle(row)">
              {{ row.state === 1 ? "禁用" : "启用" }}
            </el-button>
            <el-button
              type="text"
              :disabled="row.state === 1"
              @click="editArticles(row)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="row.state === 1"
              @click="delArticle(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        style="margin-top: 20px; text-align: right"
        background
        :current-page="reqParams.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="reqParams.pagesize"
        layout="prev, pager, next,sizes, jumper"
        :total="total"
        hide-on-single-page
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
      <!-- 视频 -->
      <div v-if="videoURL" class="videoPlay">
        <div class="close" @click="closeVideo()"><span class="el-icon-close" /></div>
        <div class="videobox"><video ref="videoRef" :src="videoURL" /></div>
      </div>

      <!-- 新增 -->
      <articles-add
        ref="addRef"
        :add-dialog-visible.sync="addDialogVisible"
        :edit-article="editArticle"
        @updateArticle="updateArticle"
      />
      <!-- 预览 -->
      <preview-container
      :preview-dialog-visible.sync = "previewDialogVisible"
      :preview="preview"
      />
    </el-card>
  </div>
</template>
<script>
import { list, changeState, remove } from '../../api/hmmm/articles'
import ArticlesAdd from '../components/articles-add.vue'
import PreviewContainer from '../components/articles-preview.vue'
export default {
  components: { ArticlesAdd, PreviewContainer },
  data () {
    return {
      // 表格数据
      tableArticlesList: [],
      // 请求的参数--表单数据
      reqParams: {
        page: 1,
        pagesize: 10,
        keyword: null,
        state: null
      },
      // 总条数
      total: 0,
      // 视频地址
      videoURL: null,
      // 控制新增对话框的布尔值
      addDialogVisible: false,
      editArticle: {}, // 当前点击的文章
      previewDialogVisible: false,
      preview: {}
    }
  },
  created () {
    this.getArticleList()
  },
  methods: {
    // 获取文章列表
    async getArticleList () {
      const {
        data: { items, counts }
      } = await list(this.reqParams)
      // console.log(res)
      this.tableArticlesList = items
      this.total = counts
      // console.log(items)
    },
    // 分页条数发生改变的时候
    handleSizeChange (pageSize) {
      this.reqParams.page = 1
      this.reqParams.pagesize = pageSize
      this.getArticleList()
    },
    // 当前页数发生变化的时候
    handleCurrentChange (newPage) {
      this.reqParams.page = newPage
      this.getArticleList()
    },
    // 序号
    indexMethod (index) {
      return index + 1 + (this.reqParams.page - 1) * this.reqParams.pagesize
    },
    // 点击清除按钮清空关键字
    delKeyWords () {
      // console.log(111)
      this.reqParams = {
        page: 1,
        pagesize: 10,
        keywords: null,
        state: null
      }
    },
    // 筛选---搜索
    search () {
      this.reqParams.page = 1
      this.getArticleList()
    },

    // 切换状态
    async stateToggle (data) {
      data.state = data.state === 1 ? 0 : 1
      await changeState({
        id: data.id,
        state: data.state
      })
      this.$message.success('操作成功')
      // console.log(res)
    },
    // 新增/修改文章的弹框
    addArticle () {
      this.addDialogVisible = true
      // console.log(id)
    },
    // 新增
    updateArticle () {
      this.getArticleList()
    },
    // 编辑
    editArticles (data) {
      this.addDialogVisible = true
      this.editArticle = data
      this.$refs.addRef.getArtiaclDetail(data.id)
    },
    // 删除
    async delArticle (row) {
      // console.log(data)
      const confirmRes = await this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => err)
      console.log(confirmRes)
      // 判断是否取消删除
      if (confirmRes === 'cancel') return this.$message('您取消了删除')
      // 调用删除接口
      await remove(row)
      // console.log(res)
      // 刷新页面
      this.getArticleList()
      //  成功提示
      this.$message.success('删除成功')
      // 如果删除的是当前页的最后一条数据 页码减一
      // 如果删除的是第一页的最后一条数据 停留在第一页
      if (this.tableArticlesList.length === 1) {
        this.reqParams.page--
        if (this.reqParams.page <= 0) {
          this.reqParams.page = 1
        }
      }
    },
    // 预览
    previewArticle (row) {
      this.previewDialogVisible = true
      this.preview = row
    },
    // 点击视频按钮播放
    openVideo (url) {
      this.videoURL = url
      this.$nextTick(() => {
        this.$refs.videoRef.play()
      })
    },
    // 点击关闭按钮视频暂停
    closeVideo () {
      this.$refs.videoRef.pause()
      this.videoURL = null
    }
  }

}

</script>

<style scoped lang='scss'>
.articles-container {
  padding: 10px;
  .el-icon-film {
    color: blue;
    font-size: 18px;
  }
  .videoPlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.3);
    overflow: hidden;
    z-index: 9999;
    .close {
      position: absolute;
      top: 30px;
      left: 50%;
      transform: translateX(-50%);
      width: 50px;
      height: 50px;
      border-radius: 50%;
      background-color: rgba(0, 0, 0, 0.4);
      line-height: 50px;
      text-align: center;
      font-size: 20px;
      color: #fff;
    }
    .videobox {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 800px;
      height: 600px;
      video {
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>
